package com.apps.sworld;

import com.apps.sannotation.security.Authority;
import com.apps.smodule.Security.model.MethodPaths;
import com.apps.smodule.Security.utils.MethodPathUtils;
import com.apps.sutils.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 所有接口的父类
 */
public interface sApiController extends sWorld {

    /**
     * 获取接口的所有方法路径
     * @return
     */
     default Map<String, MethodPaths> MethodPaths()
     {

         Map<String, MethodPaths> result=new HashMap<>();
         List<String> path=new ArrayList<>();
         RequestMapping requestMapping=this.getClass().getAnnotation(RequestMapping.class);
         if(requestMapping!=null)
         {
             for(int i=0;i<requestMapping.value().length;i++)
                 path.add(requestMapping.value()[i]);
         }
         Method[] methods = this.getClass().getMethods();
         for(int i=0;i<methods.length;i++)
         {
             PostMapping postMapping=methods[i].getAnnotation(PostMapping.class);
             if(null != postMapping)
             {
                 //获取权限级别,如果没有默认为0
                 Authority authority=methods[i].getAnnotation(Authority.class);
                 ApiOperation apiOperation=methods[i].getAnnotation(ApiOperation.class);
                 String name=postMapping.name();
                 if(null!=apiOperation)
                     name=apiOperation.value();
                 result.put(this.getClass().getSimpleName()+"_"+methods[i].getName(),new MethodPaths(name, null==authority?0:authority.level(),MethodPathUtils.CalculationPath(path,postMapping.value())));
             }
         }
         return result;
     }

}
